<script setup>
import { ArrowRight } from "@element-plus/icons-vue";
import { getCategoryListApi } from "@/api/category";
import { useRoute } from "vue-router";
import { ref, onMounted } from "vue";
import HomeBanner from "../Home/components/HomeBanner.vue";
import GoodsItem from "../Home/components/GoodsItem.vue";
import { onBeforeRouteUpdate } from "vue-router";

const route = useRoute();
//获取一级分类列表
const categoryData = ref({});
const getCategoryList = async (id = route.params.id) => {
  const res = await getCategoryListApi(id);
  categoryData.value = res.result;
  // console.log(categoryData.value);
};
onMounted(getCategoryList);
onBeforeRouteUpdate((to) => {
  getCategoryList(to.params.id);
});
</script>

<template>
  <div class="category container">
    <div class="bread">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ categoryData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="banner">
      <HomeBanner></HomeBanner>
    </div>
    <div class="all-category">
      <h3>全部分类</h3>
      <ul>
        <li v-for="item in categoryData.children" :key="item.id">
          <router-link :to="`/category/sub/${item.id} `">
            <img :src="item.picture" alt="" />
            <span>{{ item.name }}</span>
          </router-link>
        </li>
      </ul>
    </div>
    <ul class="oto">
      <li v-for="item in categoryData.children" :key="item.id">
        <div class="pro">
          <h3>{{ item.name }}</h3>
          <ul class="pp">
            <GoodsItem v-for="goods in item.goods" :key="goods.id" :goods="goods" />
          </ul>
        </div>
      </li>
    </ul>
  </div>
</template>

<style lang="scss" scoped>
.category {
  height: 4000px;
  display: flex;
  flex-direction: column;

  .bread {
    height: 70px;
    line-height: 70px;
    padding: 25px 0;
    z-index: 99;
  }
  .banner {
    height: 570px;
    position: absolute;
    top: 40px;
    left: 0;
  }
  .all-category {
    width: 100%;
    background-color: #fff;
    height: 280px;
    position: absolute;
    top: 570px;
    left: 0;
    h3 {
      height: 100px;
      line-height: 100px;
      text-align: center;
      font-size: 28px;
      color: #333;
    }
    ul {
      display: flex;
      justify-content: space-between;
      height: 180px;
      li {
        width: 170px;
        height: 150px;
        text-align: center;
        color: #333;
        img {
          width: 100px;
          height: 100px;
          margin-bottom: 10px;
        }
        span {
          display: block;
          font-size: 16px;
          margin-top: 10px;
          height: 40px;
          line-height: 40px;
        }
        &:hover {
          color: pink;
        }
      }
    }
  }
  .oto {
    background-color: #fff;
    width: 100%;
    height: 1000px;
    position: absolute;
    top: 850px;
    left: 0;
    li {
      margin-top: 10px;
      width: 100%;
      height: 400px;

      .pro {
        margin-top: 10px;
        width: 100%;
        height: 400px;
        h3 {
          height: 100px;
          line-height: 100px;
          text-align: center;
          font-size: 28px;
          color: #333;
        }
        ul {
          display: flex;
          justify-content: space-between;
          height: 180px;
          li {
            width: 170px;
            height: 150px;
            text-align: center;
            color: #333;
          }
        }
        .pp {
          display: flex;
          height: 400px;
          li {
            height: 400px;
            width: 240px;
            position: relative;
            overflow: hidden;
            .pro {
              padding: 10px 20px;
              img {
                width: 180px;
                height: 180px;
                margin: 0;
                text-align: center;
                align-items: center;
              }
              p {
                height: 40px;
                text-align: left;
                align-items: center;
              }
              .name {
                color: black;
                font-size: 16px;
              }
              .desc {
                color: rgb(132, 132, 132);
                font-size: 14px;
              }
              .price {
                font-size: 24px;
                color: brown;
              }
            }
          }
        }
      }
    }
  }
}
</style>
